package com.younion.action.consumer.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.IFavoritesService;
import com.younion.business.IFavourableEntityService;
import com.younion.business.IFavourableService;
import com.younion.common.constants.SysConstants;
import com.younion.model.FavDetailedResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.util.config.ConfigHelper;
import com.younion.vo.API_FavourableEntityVO;
import com.younion.vo.FavourableVO;

public class FavourableDetailedGetConsumer extends AbstractRequestConsumer {

	private IFavourableService favourableService;
	private IFavoritesService favoritesService;

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return getFavourable(request, response);
	}

	public String getFavourable(HttpServletRequest request,
			HttpServletResponse response) {
		String user_token = request.getParameter("user_token");
		String fav_id = request.getParameter("fav_id");
		if (!HttpRequestUtils.checkNotEmptyParam("user_token", user_token,
				response)) {
			return null;
		}
		if (!HttpRequestUtils.checkNotEmptyParam("fav_id", fav_id, response)) {
			return null;
		}
		if (!HttpRequestUtils.checkIntegerParam("fav_id", fav_id, response)) {
			return null;
		}
		int int_page = ApiConstant.API_DEFAULT_PAGE;
		ResultObject resultObject = new ResultObject();
		try {
			String img_url_pre = ConfigHelper.fastGetConfig("interface",
					"img_url_pre", "http://localhost:8080/younonmember/");
			FavourableVO favourable = favourableService.getById(fav_id);
			FavDetailedResult favDetailedResult = new FavDetailedResult();
			if (favourable != null) {
				API_FavourableEntityVO api_favourableEntityVO = favourable
						.getApi_favourableEntity();
				favDetailedResult.setCat_id(api_favourableEntityVO.getCat_id());
				favDetailedResult.setFav_id(favourable.getId().toString());
				favDetailedResult.setFav_name(api_favourableEntityVO.getName());
				favDetailedResult.setFav_detail(api_favourableEntityVO
						.getDetail());
				favDetailedResult.setNonactivatedPic(img_url_pre
						+ SysConstants.FavourableEntity_IMG_URL + "/"
						+ api_favourableEntityVO.getId() + "/"
						+ api_favourableEntityVO.getNonactivatedPic());
				favDetailedResult.setActivatedPic(img_url_pre
						+ SysConstants.FavourableEntity_IMG_URL + "/"
						+ api_favourableEntityVO.getId() + "/"
						+ api_favourableEntityVO.getActivatedPic());
				SimpleDateFormat sf = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				favDetailedResult.setValid_time_from(api_favourableEntityVO
						.getValidTimeFrom());
				favDetailedResult.setValid_time_to(api_favourableEntityVO
						.getValidTimeTo());
				favDetailedResult.setShops(api_favourableEntityVO.getShops());
				favDetailedResult.setMoney(api_favourableEntityVO.getValue());
				favDetailedResult.setFav_code(favourable.getCode());
				favDetailedResult.setState(favourable.getState().toString());
				Date useTime = favourable.getUseTime();
				if (useTime != null) {
					favDetailedResult.setUseTime(sf.format(useTime));
				} else {
					favDetailedResult.setUseTime("");
				}
				favDetailedResult.setGainTime(sf.format(favourable
						.getGainTime()));
				favDetailedResult.setApply_date_from((api_favourableEntityVO
						.getApplyBegDate() == null) ? "" : sf
						.format(api_favourableEntityVO.getApplyBegDate()));
				favDetailedResult.setApply_date_to((api_favourableEntityVO
						.getApplyEndDate() == null) ? "" : sf
						.format(api_favourableEntityVO.getApplyEndDate()));
				favDetailedResult.setUse_date_from((api_favourableEntityVO
						.getUseBegDate() == null) ? "" : sf
						.format(api_favourableEntityVO.getUseBegDate()));
				favDetailedResult.setUse_date_to((api_favourableEntityVO
						.getUseEndDate() == null) ? "" : sf
						.format(api_favourableEntityVO.getUseEndDate()));
				if (api_favourableEntityVO.getParticipate_num() != null) {
					favDetailedResult.setParticipate_num(api_favourableEntityVO
							.getParticipate_num().toString());
				} else {
					favDetailedResult.setParticipate_num("0");
				}
				String is_favorites = "0";
				if (user_token != null && !"".equals(user_token)) {
					Map map = new HashMap();
					map.put("userToken", user_token);
					map.put("favourId", Integer.valueOf(api_favourableEntityVO
							.getId()));
					map.put("type", 2);
					is_favorites = Integer.valueOf(
							favoritesService.getCountByMap(map)).toString();
				}
				favDetailedResult.setIs_favorites(is_favorites);
				favDetailedResult.setFav_ent_id(api_favourableEntityVO.getId()
						.toString());
				favDetailedResult.setGainManner(favourable.getGainManner().toString());
				favDetailedResult.setIsAuthenByMoblie(api_favourableEntityVO.getIsAuthenByMoblie());
			}

			resultObject.setResult(favDetailedResult);
			resultObject.setStatus(ApiConstant.SUCCESS_STATUS);

		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			e.printStackTrace();
		}
		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	public void setFavourableService(IFavourableService favourableService) {
		this.favourableService = favourableService;
	}

	public void setFavoritesService(IFavoritesService favoritesService) {
		this.favoritesService = favoritesService;
	}

}
